/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kason.reconnect;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

/**
 * @author 卢清波
 * @date 2021/1/25 13:23
 */
public class ReconnectClient
{
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup(10);
    Bootstrap bootstrap = null ;
    String host = "localhost";
    int port = 9000 ;

    public void init() throws InterruptedException
    {

            bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>()
                    {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception
                        {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new StringDecoder(Charset.forName("UTF-8")));
                            pipeline.addLast(new StringEncoder(Charset.forName("UTF-8")));
                            pipeline.addLast(new ReconnectClientHandler(ReconnectClient.this));
                        }
                    });
            connect();


    }


    public void connect() throws InterruptedException
    {
        // 去掉同步等待，添加监听事件，监听是否连接完成
        ChannelFuture connect = bootstrap.connect(host, port);
        connect.addListener(new ChannelFutureListener()
        {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception
            {
                if (!future.isSuccess())
                {
                    future.channel().eventLoop().schedule(
                            ()->
                            {
                               System.out.println("重新连接服务端。。。。");
                                try
                                {
                                    connect();
                                }
                                catch (InterruptedException e)
                                {
                                    e.printStackTrace();
                                }
                            },3, TimeUnit.SECONDS
                    );
                }else{
                    System.out.println("连接服务端成功");
                }
            }
        });
        connect.channel().closeFuture().sync();
    }


    public static void main(String[] args) throws InterruptedException
    {
        ReconnectClient re = new ReconnectClient();
        re.init();
    }




}
